package com.example.opengldemo.vertex_shader_more.file_version_1;
import static com.example.opengldemo.vertex_shader_more.file_version_1.ParticleDataConstant.lock;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.content.res.Resources;
import android.opengl.GLES30;

import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.ShaderUtil;

 class ParticleForDraw
{	
	int mProgram;
    int muMVPMatrixHandle;
    int muLifeSpan;
    int muBj;
    int muStartColor;
    int muEndColor;
    int maPositionHandle;
    int maTexCoorHandle;

    String mVertexShader;
    String mFragmentShader;
	
	FloatBuffer mVertexBuffer;
	FloatBuffer mTexCoorBuffer;

    int vCount=0;
    float halfSize;
    
    public ParticleForDraw(Resources res, float halfSize)
    {
    	this.halfSize=halfSize;
    	initShader(res);
    }
    public void initShader(Resources res){
        mVertexShader=ShaderUtil.loadFromAssetsFile("fragmentmagical/vertex_particle.sh", res);
        mFragmentShader= ShaderUtil.loadFromAssetsFile("fragmentmagical/frag_particle.sh", res);
        mProgram =  ShaderUtil.createProgram(mVertexShader, mFragmentShader);
        maPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");
        maTexCoorHandle= GLES30.glGetAttribLocation(mProgram, "aTexCoor");
        muMVPMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMVPMatrix");
        muLifeSpan=GLES30.glGetUniformLocation(mProgram, "maxLifeSpan");
        muBj=GLES30.glGetUniformLocation(mProgram, "bj");
        muStartColor=GLES30.glGetUniformLocation(mProgram, "startColor");
        muEndColor=GLES30.glGetUniformLocation(mProgram, "endColor");
    }
    //更新顶点数据
    public void updatVertexData(float[] points)
    {
    	mVertexBuffer.clear();
        mVertexBuffer.put(points);
        mVertexBuffer.position(0);
    }
    
    // 初始化顶点数据 和 纹理顶点数据
    public void initVertexData(float[] points)
    {

    	vCount=points.length/4;
        float vertices[]=points;
        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);

    	 float texCoor[]=new float[vCount*2];
         for(int i=0;i<vCount/6;i++)
         {
         	texCoor[12*i]=0;
         	texCoor[12*i+1]=0;
         	texCoor[12*i+2]=0;
         	texCoor[12*i+3]=1;
         	texCoor[12*i+4]=1;
         	texCoor[12*i+5]=0;
         	
         	texCoor[12*i+6]=1;
         	texCoor[12*i+7]=0;
         	texCoor[12*i+8]=0;
         	texCoor[12*i+9]=1;
         	texCoor[12*i+10]=1;
         	texCoor[12*i+11]=1;
         }
         

         ByteBuffer cbb = ByteBuffer.allocateDirect(texCoor.length*4);
         cbb.order(ByteOrder.nativeOrder());
         mTexCoorBuffer = cbb.asFloatBuffer();
         mTexCoorBuffer.clear();
         mTexCoorBuffer.put(texCoor);
         mTexCoorBuffer.position(0);
    }

    // 绘制
    public void drawSelf(int texId,float[] startColor,float[] endColor,float maxLifeSpan)
    {        

    	 GLES30.glUseProgram(mProgram);  
         GLES30.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);

         GLES30.glUniform1f(muLifeSpan, maxLifeSpan);
         GLES30.glUniform1f(muBj, halfSize*60);

         GLES30.glUniform4fv(muStartColor, 1, startColor, 0);
         GLES30.glUniform4fv(muEndColor, 1, endColor, 0);

         GLES30.glVertexAttribPointer
         (
        		maTexCoorHandle, 
         		2, 
         		GLES30.GL_FLOAT, 
         		false,
                2*4,   
                mTexCoorBuffer
         );
         GLES30.glEnableVertexAttribArray(maPositionHandle);  
         GLES30.glEnableVertexAttribArray(maTexCoorHandle);  

         GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
         GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texId);
         
         synchronized(lock)
     	 {
             GLES30.glVertexAttribPointer  
             (
             		maPositionHandle,   
             		4, 
             		GLES30.GL_FLOAT, 
             		false,
                    4*4,   
                    mVertexBuffer
             );
             GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, vCount);
     	 } 
    }
}
